﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Lottery.Herper;
using Lottery.MediaData.Core;

namespace Lottery.MediaData.UI {
    /// <summary>
    /// 过滤表 窗体
    /// </summary>
    public partial class FilterTableForm : Form {
        private Dictionary<string, FTCellData> m_fts;
        private FTCellData m_ftAll = null;

        public FilterTableForm(Dictionary<string, FTCellData> fts, string key)
            : this() {
            m_fts = fts;

            BuildFtAll();

            var index = from item in m_fts.Keys
                        orderby item ascending
                        select item;

            listBoxOrder.Items.Clear();
            listBoxOrder.Items.Add("全部");
            foreach (var item in index) {
                listBoxOrder.Items.Add(item);
            }

            if (string.IsNullOrEmpty(key))
                listBoxOrder.SelectedIndex = 0;
            else
                listBoxOrder.SelectedIndex = listBoxOrder.Items.IndexOf(key);
        }

        private void BuildFtAll() {
            m_ftAll = new FTCellData();
            var index = from item in m_fts.Keys
                        orderby item ascending
                        select item;
            foreach (var item in index) {
                var ft = m_fts[item];
                m_ftAll.AddFtCell(ft);
            }

            buttonExport.Enabled = m_ftAll.RowCount != 0;
        }

        private string m_qiShu;

        public string QiShu {
            get { return m_qiShu; }
            set {
                m_qiShu = value;
                labelInfo.Text = string.Format("足彩期数: {0}", m_qiShu);
            }
        }

        public string CC { get; set; }

        public int MaxCount { get; set; }

        public int CCIndex { get; set; }

        private FilterTableForm() {
            InitializeComponent();

            this.SetDialogStyle();

            ftGrid.InitGrid();
        }

        public void SetGridData(FTCellData data) {
            ftGrid.SetGridData(data);
        }

        private void ShowFilterTableForm_Shown(object sender, EventArgs e) {
            ftGrid.AutoResizeColumns();
        }

        private void buttonExport_Click(object sender, EventArgs e) {
            // 导出
            if (m_ftAll.RowCount == 0) {
                this.ShowInfo("没有数据可以导出");
                return;
            }

            ETHelper.Export(QiShu, CC, MaxCount, CCIndex, m_ftAll);

            ftGrid.Focus();
        }

        private void listBoxOrder_SelectedIndexChanged(object sender, EventArgs e) {
            buttonClearOne.Enabled = listBoxOrder.SelectedIndex != 0;
            buttonLoad.Enabled = listBoxOrder.SelectedIndex > 0;
            if (listBoxOrder.SelectedIndex == -1) {
                return;
            }

            // 全部
            if (listBoxOrder.SelectedIndex == 0) {
                ftGrid.SetGridData(m_ftAll);
                return;
            }

            var key = listBoxOrder.Items[listBoxOrder.SelectedIndex].ToString();
            var ft = m_fts[key];
            ftGrid.SetGridData(ft);
        }

        private void buttonClearOne_Click(object sender, EventArgs e) {
            if (this.ShowYesNoInfo("请确认是否清空当前选择的过滤表?") != DialogResult.Yes) {
                ftGrid.Focus();
                return;
            }

            var index = listBoxOrder.SelectedIndex;
            var key = listBoxOrder.Items[index].ToString();
            listBoxOrder.Items.RemoveAt(index);
            m_fts.Remove(key);
            BuildFtAll();

            listBoxOrder.SelectedIndex = index < listBoxOrder.Items.Count ? index : index - 1;
        }

        private void buttonClearAll_Click(object sender, EventArgs e) {
            // 清空所有过滤表
            if (this.ShowYesNoInfo("请确认是否清空所有的过滤表?") != DialogResult.Yes) {
                ftGrid.Focus();
                return;
            }

            m_fts.Clear();
            BuildFtAll();
            listBoxOrder.Items.Clear();
            listBoxOrder.Items.Add("全部");
            listBoxOrder.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e) {
            if (m_ftAll.RowCount == 0) {
                this.ShowInfo("没有数据无法进行模拟开奖");
                return;
            }

            var form = new SimulationForm();
            form.Numbers = DataHelper.LoadNumbers(m_qiShu);

            if (form.ShowDialog() == DialogResult.OK) {
                var numbers = form.Numbers;
                //保存奖号
                DataHelper.SaveNumbers(m_qiShu, numbers);

                foreach (var item in m_fts.Values) {
                    DataHelper.DoSimulation(numbers, item);
                }

                BuildFtAll();

                var key = listBoxOrder.Items[listBoxOrder.SelectedIndex].ToString();
                var ft = listBoxOrder.SelectedIndex == 0 ? m_ftAll : m_fts[key];
                ftGrid.SetGridData(ft);
            }
        }

        private void buttonEditLimit_Click(object sender, EventArgs e) {
            // 修改限制范围
            var rowIndex = ftGrid.CurrentRow.Index;
            var v = ftGrid[15, rowIndex].Value.ToString();
            var up = 0;
            for (int i = 1; i < 15; i++) {
                var c = ftGrid[i, rowIndex].Value;
                if (c != null && !string.IsNullOrEmpty(c.ToString())) up++;
            }
            var form = new EditLimitForm(v, up);
            if (form.ShowDialog() == DialogResult.OK) {
                ftGrid[15, rowIndex].Value = form.Limit;
                var key = listBoxOrder.Items[listBoxOrder.SelectedIndex].ToString();
                var ft = m_fts[key];
                ft[15, rowIndex] = form.Limit;
                BuildFtAll();
            }
        }

        private void ftGrid_RowEnter(object sender, DataGridViewCellEventArgs e) {
            var v = ftGrid[17, e.RowIndex].Value.ToString();
            buttonEditLimit.Enabled = v.Contains("合并");
        }

        private void button2_Click(object sender, EventArgs e) {
            // 调入限制表

            if (listBoxOrder.SelectedIndex == -1) {
                this.ShowInfo("请选择过滤表");
                return;
            }

            if (listBoxOrder.SelectedIndex == 0) {
                this.ShowInfo("请选择过滤表");
                return;
            }

            if (openFileDialog.ShowDialog() != DialogResult.OK) {
                return;
            }
            var fileName = openFileDialog.FileName;
            var info = string.Format("请确认是否导入 {0} 中的数据?", fileName);
            if (this.ShowYesNoInfo(info) != DialogResult.Yes) {
                return;
            }

            var order = listBoxOrder.Items[listBoxOrder.SelectedIndex].ToString();
            var data = m_fts[order];
            
            if (order.StartsWith("1")) {
                // 表1
                info = ETHelper.ImportLimitTable1(fileName, data);
            } else if (order.StartsWith("2")) {
                // 表2
                info = ETHelper.ImportLimitTable2(fileName, data);
            } else if (order.StartsWith("3")) {
                // 表3
                info = ETHelper.ImportLimitTable3(fileName, data);
            } else if (order.StartsWith("4")) {
                // 表4
                info = ETHelper.ImportLimitTable4(fileName, data);
            }

            ftGrid.SetGridData(data);

            BuildFtAll();

            this.ShowInfo(info);
        }
    }
}
